﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Stock.物品类型
{
    public partial class CategoryAddAndEdit : Form
    {
        //用于窗体间传递值
        public int Id;
        public string CName;
        
        public Category f;//桥梁
        public CategoryAddAndEdit()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //非空判断
            if (textBox1.Text == "" )
            {
                MessageBox.Show("类型不可为空");
            }
            else
            {
                string CName = textBox1.Text;
               
                string sql = $"insert into Category values('{CName}')";
                if (DBH.NoSelect(sql) > 0)
                {
                    MessageBox.Show("新增成功");
                    f.dataGridView1.AutoGenerateColumns = false;
                    f.dataGridView1.DataSource = DBH.Select("select * from Category");
                }
                else
                {
                    MessageBox.Show("新增失败");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "" )
            {
                MessageBox.Show("类型不能为空");
            }
            else
            {
                //获取最新值
                
                string CName = textBox1.Text;
               


                string sql = $"update Category set CName='{1}' where Id={Id}";
                if (DBH.NoSelect(sql) > 0)
                {
                    MessageBox.Show("保存成功！");
                    f.dataGridView1.AutoGenerateColumns = false;
                    f.dataGridView1.DataSource = DBH.Select("select * from Category");
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
